package com.authorwjf;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class Main extends Activity implements SensorEventListener {
	
	private float mLastX, mLastY, mLastZ;
	private boolean mInitialized;
	private SensorManager mSensorManager;
    private Sensor mAccelerometer;
    private final float NOISE = (float) 2.0;
    private int sensorType = Sensor.TYPE_ACCELEROMETER;
	 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        System.out.println("SensorEventListener onCreate");
        mInitialized = false;
        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        if(mSensorManager ==null){
        	System.out.println("SensorManager is null");
        }
        else{
        	List<Sensor> sensors = mSensorManager.getSensorList(Sensor.TYPE_ALL);
        	System.out.println("Sensor list:");
        	for (Sensor sensor : sensors){
        		System.out.println(sensor.getType()+" "+sensor.getName());
        	
        	}
        	
        	sensors = mSensorManager.getSensorList(sensorType);
            System.out.println("Sensor.TYPE Sensor list:");
        	for (Sensor sensor : sensors){
        		System.out.println(sensor.getType()+" "+sensor.getName());
        	}
        	
        }
        
        mAccelerometer = mSensorManager.getDefaultSensor(sensorType);
        if(mAccelerometer == null){
        	System.out.println("Sensor is null");
        }
        else{
        	System.out.println("Default Sensor.TYPE:");
        	System.out.println(mAccelerometer.getType()+" "+mAccelerometer.getName());
        	
        }
        
    }

    protected void onResume() {
        super.onResume();
        boolean bool = mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
        System.out.println("onResume mSensorManager.registerListener: "+ Boolean.toString(bool));
        
    }

    protected void onPause() {
        super.onPause();
        mSensorManager.unregisterListener(this);
        System.out.println("onPause mSensorManager.unregisterListener");
    }
    
    private String parseAccuracy(int accuracy){
    	//converts accuracy from a constant value into a string that the value represents 
		String string = "";
		if(accuracy == SensorManager.SENSOR_STATUS_ACCURACY_HIGH){
			string = "high";
		}
		else if(accuracy == SensorManager.SENSOR_STATUS_ACCURACY_MEDIUM){
			string = "medium";
		}
		else if(accuracy == SensorManager.SENSOR_STATUS_ACCURACY_LOW){
			string = "low";
		}
		else if(accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE){
			string = "unreliable";
		}
		return string;
    }
    
	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		System.out.println(sensor.getName()+ " accuracy changed to: "+ parseAccuracy(accuracy));
	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		
		System.out.println("onSensorChanged sensor: "+event.sensor.getName());
		
		int accuracy = event.accuracy;
		System.out.println("onSensorChanged accuracy: "+parseAccuracy(accuracy));
		
		float[] vals = event.values;
		float x = vals[0];
		float y = vals[1];
		float z = vals[2];
		
		System.out.println("onSensorChanged values: x:"+Float.toString(x) + " y:" + Float.toString(y) + " z:" + Float.toString(z));
		
		//changes text and sets graphic based on acceleration
		TextView tvX= (TextView)findViewById(R.id.x_axis);
		TextView tvY= (TextView)findViewById(R.id.y_axis);
		TextView tvZ= (TextView)findViewById(R.id.z_axis);
		ImageView iv = (ImageView)findViewById(R.id.image);
		if (!mInitialized) {
			mLastX = x;
			mLastY = y;
			mLastZ = z;
			tvX.setText("0.0");
			tvY.setText("0.0");
			tvZ.setText("0.0");
			mInitialized = true;
		} else {
			float deltaX = Math.abs(mLastX - x);
			float deltaY = Math.abs(mLastY - y);
			float deltaZ = Math.abs(mLastZ - z);
			if (deltaX < NOISE) deltaX = (float)0.0;
			if (deltaY < NOISE) deltaY = (float)0.0;
			if (deltaZ < NOISE) deltaZ = (float)0.0;
			mLastX = x;
			mLastY = y;
			mLastZ = z;
			tvX.setText(Float.toString(deltaX));
			tvY.setText(Float.toString(deltaY));
			tvZ.setText(Float.toString(deltaZ));
			iv.setVisibility(View.VISIBLE);
			if (deltaX > deltaY) {
				iv.setImageResource(R.drawable.horizontal);
			} else if (deltaY > deltaX) {
				iv.setImageResource(R.drawable.vertical);
			} else {
				iv.setVisibility(View.INVISIBLE);
			}
		}
	}
}